﻿
'not maintain now, the newest version is in visitCounter project
Imports System.IO
Imports storoomc
Imports xmlctrl
Imports xhtmlctrl
Imports webctrl

Public Class counter
    Inherits controlCounter

    Private host As String
    Private port As Int32
    Private retrytimes As Int64
    Private adminlink As String

    Private Function getstoroom(ByRef host As String, ByRef port As Int32, ByRef retrytimes As Int64) As Boolean
        copy(host, Me.host)
        copy(port, Me.port)
        copy(retrytimes, Me.retrytimes)
        Return True
    End Function

    Public Overrides Sub initial(Optional ByVal config As configParser = Nothing)
        MyBase.initial(config)
        host = config.parameter(configNaming.counter.name, configNaming.counter.storoomhost _
                                , configNaming.defaultValue.counter.storoomhost)
        port = config.parameter(configNaming.counter.name, configNaming.counter.storoomport _
                                 , configNaming.defaultValue.counter.storoomport)
        retrytimes = config.parameter(configNaming.counter.name, configNaming.counter.storoomretrytimes _
                                       , configNaming.defaultValue.counter.storoomretrytimes)
        adminlink = config.parameter(configNaming.counter.name, configNaming.counter.adminlink _
                                      , configNaming.defaultValue.counter.adminlink)
        storoom.initial(AddressOf getstoroom)
    End Sub

    Protected Overrides Function read(ByVal key As String, ByRef count As Long) As Boolean
        Dim buff As bytes = Nothing
        If storoom.read(key, buff) Then
            If Not convertor.toint64(buff, count) Then
                raiseError("cannot convert storoom output to int64, ignore request. key " + key _
                           , errorHandle.errorType.warning)
                Return False
            Else
                Return True
            End If
        Else
            raiseError("cannot read key " + key + " from storoom, ignore request.", errorHandle.errorType.warning)
            Return False
        End If
    End Function

    Protected Overrides Function write(ByVal key As String, ByVal count As Long) As Boolean
        Dim buff As bytes = Nothing
        If Not convertor.tobytes(count, buff) Then
            raiseError("cannot convert count to byte array, ignore request. key " + key _
                       + ", count " + Convert.ToString(count), errorHandle.errorType.warning)
            Return False
        Else
            If Not storoom.modify(key, buff) Then
                raiseError("cannot write back to storoom, key " + key + ", count " + Convert.ToString(count) _
                           , errorHandle.errorType.warning)
                Return False
            Else
                Return True
            End If
        End If
    End Function

    Protected Overloads Overrides Function processHomepage(ByVal context As httpContext _
                                                           , ByRef control As xmlcontrol) As Boolean
        Dim page As xhtmlpage = Nothing
        page = context.createControl(New xhtmlpage(Nothing))
        page.title().text = applicationSign() + " - counter"

        Dim label As label = Nothing
        label = context.createControl(New label(Nothing))
        label.text = "not maintain ..."
        page.body().addchild(label)
#If RETIRED Then
        Dim label As label = Nothing
        label = context.createControl(New label(Nothing))
        label.text = "welcome to use request counter, you can generate a key here, " _
                    + applicationSign() + " can create a guid as key for you, if no key url parameter in the request."
        page.body().addchild(label)

        page.body().addchild(context.createControl(New br(Nothing)))

        Dim form As form = Nothing
        form = context.createControl(New form(Nothing))
        form.action = network.thislevelPathMark
        page.body().addchild(form)

        Dim input As input = Nothing
        input = context.createControl(New input(Nothing))
        input.type = xhtmlctrl.input.type_enum.text
        input.name = keyquery
        input.value = ifoper(isemptystring(key), Guid.NewGuid().ToString(), key)
        input.style = "width:40em;"
        form.addchild(input)

        form.addchild(context.createControl(New br(Nothing)))

        If Not isemptystring(key) Then
            label = context.createControl(New label(Nothing))
            label.text = "the key you have input is invalid, please use another one."
            form.addchild(label)

            form.addchild(context.createControl(New br(Nothing)))
        End If

        input = context.createControl(New input(Nothing))
        input.type = xhtmlctrl.input.type_enum.submit
        input.value = "submit"
        form.addchild(input)
#End If

        control = page

        Return True
    End Function

    Protected Overloads Overrides Function processOutput(ByVal context As httpContext, ByVal count As Int64 _
                                                         , ByRef control As xmlcontrol) As Boolean
        Dim page As xhtmlpage = Nothing
        page = context.createControl(New xhtmlpage(Nothing))
        page.title().text = applicationSign() + " - counter result"

        Dim a As a = Nothing
        a = context.createControl(New a(Nothing))
        a.text = Convert.ToString(count)
        a.href = adminlink
        a.title = "powered by Hzj_jie - Gemini -"
        page.body().addchild(a)

        control = page

        Return True
    End Function
End Class
